<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css"  media="all">
    <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all" />
    <link rel="stylesheet" href="__CSS__/admin.css"  media="all">
    <script src="{$Think.config.httpFlag}{$Think.config.google_map_url}" async="async"></script>
    <style>
        .box{
            margin-top:20px;
            margin-bottom:10px;
        }
        #productPricing tr th,td{
            text-align:center
        }
        .required_text{
           color:red;
            padding: 8px;
        }
        table tr td{text-align: left}
        #addclassify {
            display: inline-block;
            width: 40px;
            height: 40px;
            border: 1px solid #666;
            line-height: 40px;
            border-radius: 40px;
            text-align: center;
            cursor: pointer;
            margin-top: 20px;
        }
    </style>
</head>
<body>

<div style="padding: 20px; background-color: #F2F2F2;">
    <div class="layui-row layui-col-space15" {notempty name="$goods.goods_tcode"}style="display:none"{/notempty}>
        <!--基础信息-->
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <h2>关联产品</h2>
                </div>
                <div class="layui-card-body">
                    <div class="box">
                        <div style="margin-left:20px;">
                            <form class="layui-form">
                                <div class="layui-form-item">
                                    <label class="layui-form-label"></label>
                                    <div class="layui-input-inline" style="width: 700px">
                                        <p style="color: red;"><i class="layui-icon" style="margin-right:8px;">&#xe702;</i>检测到当前商品未关联产品，请填写产品编码进行绑定。</p>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">产品编码</label>
                                    <div class="layui-input-inline" style="width: 700px">
                                        <input name="code" lay-verify="required"  placeholder="请输入产品编号" autocomplete="off" class="layui-input" type="text">
                                    </div>
                                </div>
                                <div style="margin-left:20px;">
                                    <div class="layui-input-block">
                                        <a class="layui-btn layui-btn-radius saveProductCode">保存</a>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-row layui-col-space15" {empty name="$goods.goods_tcode"}style="display:none"{/empty}>
        <!--基础信息-->
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <h2>利率设置</h2>
                </div>
                <div class="layui-card-body">
                    <div class="box">
                        <div style="margin-left:20px;">
                            <form class="layui-form" id="basic">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">包车毛利率</label>
                                    <div class="layui-input-inline" style="width: 700px">
                                        <input name="vehicle_rate" lay-verify="required" {if isset($goods.vehicle_rate)}value="{$goods.vehicle_rate}"{/if} placeholder="请输入包车毛利率" autocomplete="off" class="layui-input" type="text">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">门票毛利率</label>
                                    <div class="layui-input-inline" style="width: 700px">
                                        <input name="ticket_rate" lay-verify="required" {if isset($goods.ticket_rate)}value="{$goods.ticket_rate}"{/if} placeholder="请输入门票毛利率" autocomplete="off" class="layui-input" type="text">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">酒店毛利率</label>
                                    <div class="layui-input-inline" style="width: 700px">
                                        <input name="hotel_rate" lay-verify="required" {if isset($goods.hotel_rate)}value="{$goods.hotel_rate}"{/if} placeholder="请输入酒店毛利率" autocomplete="off" class="layui-input" type="text">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">额外毛利率</label>
                                    <div class="layui-input-inline" style="width: 700px">
                                        <input name="extra_rate" lay-verify="required" {if isset($goods.extra_rate)}value="{$goods.extra_rate}"{/if} placeholder="请输入额外费毛利率" autocomplete="off" class="layui-input" type="text">
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!--酒店信息-->
        <div class="layui-col-md12">
            <div class="layui-card"  style="padding-bottom:20px">
                <div class="layui-card-header">
                    <h2>酒店配置</h2>
                </div>
                <div class="layui-card-body">
                    <div class="box">
                        <div style="margin-left:20px;">
                            <form class="layui-form" action="">
                                <table class="layui-table" lay-even="" lay-skin="line">
                                    <colgroup>
                                        <col width="25%">
                                        <col width="25%">
                                        <col width="20%">
                                        <col width="20%">
                                        <col width="10%">
                                    </colgroup>
                                    <thead>
                                    <tr>
                                        <th>入住时间</th>
                                        <th>星级</th>
                                        <th>城市</th>
                                        <th>天数</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody id="hotelNode">
                                    {volist name='hotel_price' id='value'}
                                    <tr>
                                        <td>
                                            <select name="startday" id="startday" lay-search="" class="layui-select">
                                                <option value="">请选择</option>
                                                {volist name='startDay' id='s_value'}
                                                <option value="{$s_value.day}" {if $value['startday']==$s_value['day']}selected{/if}>{$s_value.dayName}</option>
                                                {/volist}
                                            </select>
                                        </td>
                                        <td>
                                            <select name="level" id="level" lay-search="" class="layui-select">
                                                <option value="">请选择</option>
                                                {volist name='level' id='l_value'}
                                                <option value="{$l_value.level}" {if $value['level']==$l_value['level']}selected{/if}>{$l_value.levelName}</option>
                                                {/volist}
                                            </select>
                                        </td>
                                        <td>
                                            <select name="city" id="city" lay-search="" class="layui-select">
                                                <option value="">请选择</option>
                                                {volist name='cityList' id='c_value'}
                                                <option value="{$c_value.kbx_id}" {if $c_value['kbx_id']==$value['kbxCityId']}selected{/if}>{$c_value.name}</option>
                                                {/volist}
                                            </select>
                                        </td>
                                        <td><input type="number" class="layui-input layui-input-inline" name="days" value="{$value.days}" style="width: 50%;margin-right:8px">晚</td>
                                        <td><a class='delHotelNode layui-btn layui-btn-danger layui-btn-sm'><i class="layui-icon">&#xe640;</i></a></td>
                                    </tr>
                                    {/volist}
                                    </tbody>
                                </table>
                                <div style="text-align: center;margin-bottom:10px;">
                                    <span id="addclassify" style="">
                                        <i class="layui-icon" style="font-size: 30px"></i>
                                    </span>
                                </div>
                            </form>
                        </div>
                    </div>
                    <!--<div style="margin-left:20px;">-->
                        <!--<div class="layui-input-block">-->
                            <!--<button class="layui-btn layui-btn-radius saveHotelNode">保存</button>-->
                        <!--</div>-->
                    <!--</div>-->
                </div>
            </div>
        </div>
        <!--门票信息-->
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <h2>门票配置</h2>
                </div>
                <div class="layui-card-body">
                    <div class="box">
                        <div style="margin-left:20px;">
                            <table class="layui-table" lay-even="" lay-skin="line">
                                <colgroup>
                                    <col width="30%">
                                    <col width="30%">
                                    <col width="20%">
                                    <col width="20%">
                                </colgroup>
                                <thead>
                                <tr>
                                    <th>poi名称</th>
                                    <th>标题</th>
                                    <th>数量</th>
                                    <th>采购价</th>
                                </tr>
                                </thead>
                                <tbody>
                                {volist name='ticketList' id='value' key='key'}
                                <tr>
                                    <td>{$value.poiName}</td>
                                    <td>{$value.ticketName}</td>
                                    <td>x1</td>
                                    <td>{notempty name="$value['unitBase']['currency']"}{if $value['unitBase']['currency']=='CAD'}C${elseif $value['unitBase']['currency']=='JPY'}円{else}${/if}{/notempty}{$value.unitBase.amount}</td>
                                </tr>
                                {/volist}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!--额外信息-->
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <h2>其他配置 <span style="color:red;font-size: 12px;">(货币单位以当地货币为准)</span></h2>
                </div>
                <div class="layui-card-body">
                    <div class="box">
                        <div style="margin-left:20px;">
                            <table class="layui-table" lay-even="" lay-skin="line">
                                <colgroup>
                                    <col width="25%">
                                    <col width="25%">
                                    <col width="50%">
                                </colgroup>
                                <thead>
                                <tr>
                                    <th>司导</th>
                                    <th>额外费用</th>
                                    <th>费用说明</th>
                                </tr>
                                </thead>
                                <tbody id="extraNode">
                                {volist name='extra_cost' id='value' key='key'}
                                <tr>
                                    <td>第{$key}位司导</td>
                                    <td>
                                        <input type="text" name="cost" class="layui-input" value="{$value.cost}">
                                    </td>
                                    <td>
                                        <input type="text" name="desc" class="layui-input" value="{$value.desc}">
                                    </td>
                                </tr>
                                {/volist}
                                </tbody>
                            </table>
                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <a class="layui-btn layui-btn-radius savePriceRate" lay-submit lay-filter="basic">保存</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<script src="__PUBLIC__/layui/layui.js"></script>
<script src="__PUBLIC__/jquery/jquery.min.js"></script>
<script>
    layui.use(['element','layer', 'form'], function() {
        var layer = layui.layer,
            element = layui.element,
            $ = layui.jquery,
            form = layui.form;
        //***************************************************************************
        $("#addclassify").click(function(){
            var  str = "";
            var  startdayStr = renderStartDay();
            var  levelStr = renderHotelLevel();
            var  cityStr = renderCityList();
            str += "<tr>\n" +
                "<td>"+startdayStr+"</td>\n" +
                "<td>"+levelStr+"</td>\n" +
                "<td>"+cityStr+"</td>\n" +
                "<td><input class='layui-input layui-input-inline' name='days' style='width:50%;margin-right:8px' type='number' name='days' min='1' step='1' value='1'>晚</td>\n" +
                "<td ><a class='delHotelNode layui-btn layui-btn-danger layui-btn-sm'><i class=\"layui-icon\">&#xe640;</i></a></td>\n" +
                "</tr>";
            $("#hotelNode").append(str);
            form.render('select');
        });
        function renderStartDay()
        {
            var startDay = {$startDayJson};
            var str = '';
            str += '<select name="startday" id="startday" lay-filter="level" class="layui-select" lay-search="" style="width:80%">';
            str += '<option value="">请选择</option>';
            $.each(startDay,function(i,v){
                str += '<option value="'+v.day+'">'+v.dayName+'</option>';
            });
            str += '</select>';

            return str;
        }
        function renderHotelLevel()
        {
            var levelData = {$levelJson};
            var str = '';
            str += '<select name="level" id="level" lay-filter="level" class="layui-select" lay-search="" style="width:80%">';
            str += '<option value="">请选择</option>';
            $.each(levelData,function(i,v){
                str += '<option value="'+v.level+'">'+v.levelName+'</option>';
            });
            str += '</select>';

            return str;
        }

        function renderCityList()
        {
            var citylist = {$cityJson};
            var str = '';
            str += '<select name="city" id="city" lay-filter="city" class="layui-select" lay-search="" style="width:80%">';
            str += '<option value="">请选择</option>';
            $.each(citylist,function(i,v){
                str += '<option value="'+v.kbx_id+'">'+v.name+'</option>';
            });
            str += '</select>';

            return str;
        }

        //删除酒店配置
        $(document).on('click','.delHotelNode',function(){
            $(this).parent().parent().remove();
        });
        //保存酒店数据
        function saveHotelNode(){
            //检查数据是否完整
            var list = $("#hotelNode tr");
            var errMsg = "";
            var postList = [];
            $.each(list,function(i,v){
                var hotelData = {};
                var startday = $(v).find('select[name=startday]').val();
                if(startday=="" || startday==undefined){
                    errMsg = "请补全所有的酒店入住时间";
                    return false;
                }
                var level = $(v).find('select[name=level]').val();
                if(level=="" || level==undefined){
                    errMsg = "请补全所有的酒店星级";
                    return false;
                }
                var city = $(v).find('select[name=city]').val();
                if(city=="" || city==undefined){
                    errMsg = "请补全所有的酒店城市";return false;
                }
                var days = $(v).find('input[name=days]').val();
                if(days=="" || days==undefined){
                    errMsg = "请补全所有的酒店天数";return false;
                }
                hotelData.startday = startday;
                hotelData.level = level;
                hotelData.levelName = getLevelName(level);
                hotelData.kbxCityId = city;
                hotelData.cityName = getCityName(city);
                hotelData.days = days;
                postList.push(hotelData);
            });

            if(errMsg!==""){
                layer.msg(errMsg);return false;
            }

            return postList;
        }
        //保存额外价格数据
        function saveExtraNode(){
            var list = $("#extraNode tr");
            var postList = [];
            $.each(list,function(i,v){
                var extraData = {};
                var cost = $(v).find('input[name=cost]').val();
                var desc = $(v).find('input[name=desc]').val();
                extraData.cost = cost;
                extraData.desc = desc;
                postList.push(extraData);
            })
            return postList;
        }
        //获取星级名称
        function getLevelName(level)
        {
            var levelName = "";
            var levelData = {$levelJson};
            $.each(levelData,function(i,v){
                if(v.level==level){
                    levelName = v.levelName;
                    return false;
                }
            })
            return levelName;
        }
        //获取城市名称
        function getCityName(city)
        {
            var cityName = "";
            var cityData = {$cityJson};
            $.each(cityData,function(i,v){
                if(v.kbx_id==city){
                    cityName = v.name;
                    return false;
                }
            })
            return cityName;
        }

        $(".savePriceRate").click(function(){
            var vehicle_rate = $("input[name=vehicle_rate]").val();
            var ticket_rate = $("input[name=ticket_rate]").val();
            var hotel_rate = $("input[name=hotel_rate]").val();
            var extra_rate = $("input[name=extra_rate]").val();

            if(vehicle_rate<1){
                layer.msg("用车利率不能小于1");return false;
            }
            if(ticket_rate<1){
                layer.msg("门票利率不能小于1");return false;
            }
            if(hotel_rate<1){
                layer.msg("酒店利率不能小于1");return false;
            }
            if(extra_rate<1){
                layer.msg("额外利率不能小于1");return false;
            }

            var postList = saveHotelNode();
            var extraList = saveExtraNode();
            if(!postList){
                //酒店信息不全
                return false;
            }
            var id = {$goods.goods_id};
            var postdata = JSON.stringify(postList);
            var extradata = JSON.stringify(extraList);
            var update_loading = layer.load(1);
            $.post('/admin/goods/savePriceRate',{
                id:id,
                vehicle_rate:vehicle_rate,
                ticket_rate:ticket_rate,
                hotel_rate:hotel_rate,
                extra_rate:extra_rate,
                extradata:extradata,
                postdata:postdata
            },function(data){
                layer.close(update_loading);
                layer.msg(data.msg);
                if(data.code==1){
                    setTimeout(function(){
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    },1000)
                }
            },'json')
        })
        //关联产品
        $(".saveProductCode").click(function(){
            var id = {$goods.goods_id};
            var code = $("input[name=code]").val();
            if(code==""||code==undefined){
                layer.msg("请填写产品编码");return false;
            }

            $.post('/admin/goods/setGoodsProCode',{id:id,code:code},function(data){
                layer.msg(data.msg);
                if(data.code==1){
                    setTimeout(function(){
                        window.location.reload();
                    },1000)
                }
            },'json')
        })
    });
</script>
</body>
</html>
